package com.logictech.activity.system.web.back;

import com.logictech.activity.system.entity.SystemRole;
import com.logictech.activity.system.service.SystemRoleService;
import com.logictech.activity.system.vo.LoginUser;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ContextHolderUtils;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/system/back/systemRole" })
public class SystemRoleController extends BaseController {
	@Autowired
	private SystemRoleService systemRoleService;
	
	@RequestMapping(value = { "list" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	public void list(@ModelAttribute SystemRole query, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
		PageQuery<SystemRole> pageQuery = new PageQuery<SystemRole>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		VelocityContext velocityContext = new VelocityContext();
		pageQuery.setQuery(query);
		velocityContext.put("systemRole", query);
		velocityContext.put("pageInfos",
				SystemTools.convertPaginatedList(this.systemRoleService.queryPageList(pageQuery)));
		String viewName = "system/back/systemRole-list.vm";
		request.getSession().setAttribute("pageQuerySystemRole", pageQuery);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = { "back" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	public void back(@ModelAttribute SystemRole query, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
		PageQuery<SystemRole> pageQuery = new PageQuery<SystemRole>();
		
		Object session = request.getSession().getAttribute("pageQuerySystemRole");
		if(session != null){
			pageQuery = (PageQuery<SystemRole>)session;
			query = pageQuery.getQuery();
		}else{
			pageQuery.setPageNo(pageNo);
			pageQuery.setPageSize(pageSize);
			pageQuery.setQuery(query);
		}
		VelocityContext velocityContext = new VelocityContext();
		velocityContext.put("systemRole", query);
		velocityContext.put("pageInfos",
				SystemTools.convertPaginatedList(this.systemRoleService.queryPageList(pageQuery)));
		String viewName = "system/back/systemRole-list.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	@RequestMapping(value = { "toDetail" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public void toDetail(@RequestParam(required = true, value = "id") Long id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "system/back/systemRole-detail.vm";
		SystemRole systemRole = this.systemRoleService.queryById(id);
		velocityContext.put("systemRole", systemRole);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	@RequestMapping(value = { "/toAdd" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	public void toAdd(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "system/back/systemRole-add.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	@RequestMapping(value = { "/doAdd" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public AjaxJson doAdd(@ModelAttribute SystemRole systemRole) {
		AjaxJson j = new AjaxJson();
		try {
			systemRole.setCreateDt(new Date());
			systemRole.setEditDt(new Date());
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			systemRole.setCreator(user.getId().toString());
			systemRole.setEditor(user.getId().toString());
			this.systemRoleService.doAdd(systemRole);
			j.setMsg("保存成功");
		} catch (Exception e) {
			log.info(e.getMessage());
			j.setSuccess(false);
			j.setMsg("保存失败");
		}
		return j;
	}

	@RequestMapping(value = { "toEdit" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public void toEdit(@RequestParam(required = true, value = "id") Long id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		SystemRole systemRole = this.systemRoleService.queryById(id);
		velocityContext.put("systemRole", systemRole);
		String viewName = "system/back/systemRole-edit.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	@RequestMapping(value = { "/doEdit" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public AjaxJson doEdit(@ModelAttribute SystemRole systemRole) {
		AjaxJson j = new AjaxJson();
		try {
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			systemRole.setEditDt(new Date());
			systemRole.setEditor(user.getId().toString());
			this.systemRoleService.doEdit(systemRole);
			j.setMsg("编辑成功");
		} catch (Exception e) {
			log.info(e.getMessage());
			j.setSuccess(false);
			j.setMsg("编辑失败");
		}
		return j;
	}

	@RequestMapping(value = { "doDelete" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public AjaxJson doDelete(@RequestParam(required = true, value = "id") Long id) {
		AjaxJson j = new AjaxJson();
		try {
			SystemRole systemRole = this.systemRoleService.queryById(id);
			if ("00".equals(systemRole.getRoleId())) {
				j.setSuccess(false);
				j.setMsg("系统管理员角色不能删除");
			}
			this.systemRoleService.doDelete(id, systemRole.getRoleId());
			j.setMsg("删除成功");
		} catch (Exception e) {
			log.info(e.getMessage());
			j.setSuccess(false);
			j.setMsg("删除失败");
		}
		return j;
	}

	@RequestMapping(value = { "toPrivilegeTree" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.GET })
	public void toPrivilegeTree(@RequestParam(required = true, value = "id") Long id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		SystemRole systemRole = this.systemRoleService.queryById(id);
		velocityContext.put("systemRole", systemRole);
		String viewName = "system/back/systemRole-privilegetree.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}
}

